package com.wushi.lenovo.asia5b.app_config;

import com.mvp.base.model.ResultModel;
import com.mvp.common.LocalManageUtil;
import com.mvp.dagger.modules.NetworkModule;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.CountModel;
import com.wushi.lenovo.asia5b.app_model.other.CountType;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/11/28.
 */
public class InboxRepository {
    private final static InboxRepository inboxRepository = new InboxRepository();

    private static synchronized InboxRepository getInstance() {
        return inboxRepository;
    }

    private final ApiService apiService;
    private int inboxCount = 0;//消息数量
    private Subject<Integer> inboxCountSubject;
    private long lastGetInboxCountTime = 0;

    public InboxRepository() {
        inboxCountSubject = PublishSubject.create();
        this.apiService = NetworkModule.getRetrofit().create(ApiService.class);
    }

    /**
     * 从后台更新数量并通知所有界面
     */
    public static void updateInboxCount() {
        getInstance().getInboxCountByNet();
    }

    public static void setInboxCount(int inboxCount) {
        getInstance().inboxCount = inboxCount;
        getInstance().inboxCountSubject.onNext(inboxCount);
    }

    /**
     * 订阅数据监听
     *
     * @return
     */
    public static Flowable<Integer> getInboxCountOB() {
        return getInstance().inboxCountSubject.toFlowable(BackpressureStrategy.LATEST);
    }

    public static int getInboxCount() {
        return getInstance().inboxCount;
    }


    /**
     * 获取未读消息
     */
    private void getInboxCountByNet() {

        if (!UserRepository.isLogin()) {
            d("--->>>>>>>>---没登录");
            return;
        }
        if ((System.currentTimeMillis() - lastGetInboxCountTime) < 1000) {
            return;
        }
        lastGetInboxCountTime = System.currentTimeMillis();
        //普通消息未读数
        Observable<ResultModel> msgInboxCount = apiService.getMsgInboxCount(UserInfo.getMemberId(), LocalManageUtil.getAppLanguage());
        //订单消息未读数
        Observable<ResultModel<CountModel>> orderCount = apiService.getCount(CountType.Inbox_Order.getCountPath(), UserInfo.getMemberId());
        Observable.zip(msgInboxCount, orderCount, new BiFunction<ResultModel, ResultModel<CountModel>, Integer>() {
            @Override
            public Integer apply(ResultModel t1, ResultModel<CountModel> t2) throws Exception {
                int total = 0;
                if (t1.isSuccess() && t2.isSuccess()) {
                    total = t1.getCount_unread() + t2.getData().getCount();
                }
                return total;
            }
        })
                .subscribeOn(Schedulers.io())
                .subscribe(total -> {
                    inboxCount = total;
                    if (!UserRepository.isLogin()) {
                        inboxCount = 0;
                    }
                    //通知所有界面
                    inboxCountSubject.onNext(inboxCount);
                }, throwable -> {
                    throwable.printStackTrace();
                });


    }
}